package com.theaf.basemodule.dao;

import com.theaf.basemodule.common.model.SysOrg;
import com.theaf.core.repo.base.BaseRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.Date;
import java.util.List;

/**
 * @Author:songwenke
 * @Description:机构
 * @Date:create in 13:57 2018/1/29
 * @Modified By:
 **/
public interface OrganDAO extends BaseRepository<SysOrg,Long> {
    @Query(nativeQuery=true,value = "select s.* from sys_org s where s.level_code like (select t.level_code||'%' from sys_org t" +
            " where t.state<>3 and t.name like '%'||:name||'%' and (t.type=:type or :type is null)" +
            " and t.creater like '%'||:creater||'%' and t.updater like '%'||:updater||'%'" +
            " and (t.area_id=:areaId or :areaId is null) and (t.enabled=:enabled or :enabled is null)" +
            " and (t.create_time>=:start or :start is null) and (t.create_time<:end or :end is null)) " +
            " and (s.pid=:pid or :pid is null) and (:auths is null or ','||:auths||',' like '%,'||s.id||',%') order by s.order_no")
    List<SysOrg> findForTree(@Param("name") String name, @Param("type") String type, @Param("creater") String creater,@Param("updater") String updater,
                           @Param("areaId") Long areaId, @Param("enabled") Integer enabled, @Param("start") Date start,
                           @Param("end") Date end,@Param("pid") Long pid,@Param("auths") String auths);

    @Query(nativeQuery=true,value = "select s.id from sys_org s " +
            "where s.level_code like (select a.level_code from sys_org a where a.id=:id)||'%'")
    List<Long> findChildAllId(@Param("id") Long id);
    @Query(nativeQuery=true,value = "select s.id from sys_org s where s.pid=:id or s.id=:id")
    List<Long> findChildId(@Param("id") Long id);

    @Query(nativeQuery=true,value = "select s.* from sys_org s " +
            "where s.level_code like (select a.level_code from sys_org a where a.id=:id)||'%'")
    List<SysOrg> findChildAll(@Param("id") Long id);
    @Query(nativeQuery=true,value = "select s.* from sys_org s where s.pid=:id or s.id=:id")
    List<SysOrg> findChild(@Param("id") Long id);
}
